<html>
<head><meta charset="utf-8"><title>async fn trait bound · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/async.20fn.20trait.20bound.html">async fn trait bound</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="198833403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/async%20fn%20trait%20bound/near/198833403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/async.20fn.20trait.20bound.html#198833403">(May 26 2020 at 23:16)</a>:</h4>
<p>How can I write the corresponding <code>FnOnce</code> trait bound to accept functions like <code>async fn test(x: &amp;mut i32)</code>?</p>



<a name="198833439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/async%20fn%20trait%20bound/near/198833439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/async.20fn.20trait.20bound.html#198833439">(May 26 2020 at 23:17)</a>:</h4>
<p>I'm having trouble tying the anonymous lifetime of the argument <code>x</code> to the lifetime of the returned Future</p>



<a name="198852003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/async%20fn%20trait%20bound/near/198852003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Roland Kuhn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/async.20fn.20trait.20bound.html#198852003">(May 27 2020 at 05:58)</a>:</h4>
<p>does this express what you want? <code>type X = for&lt;'a&gt; FnOnce(&amp;'a mut i32) -&gt; (dyn std::future::Future&lt;Output = ()&gt; + 'a)</code></p>



<a name="198859899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/async%20fn%20trait%20bound/near/198859899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Roland Kuhn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/async.20fn.20trait.20bound.html#198859899">(May 27 2020 at 07:53)</a>:</h4>
<p>or (more towards using in <code>where</code>):</p>
<div class="codehilite"><pre><span></span><code>type X&lt;&#39;a, F: Future&lt;Output = ()&gt; + &#39;a&gt; = FnOnce(&amp;&#39;a mut i32) -&gt; F;
</code></pre></div>



<a name="199279166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/async%20fn%20trait%20bound/near/199279166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/async.20fn.20trait.20bound.html#199279166">(May 30 2020 at 22:35)</a>:</h4>
<p>That's not quite what I'm after. I managed to find a solution using an extra trait that works for standalone async functions, but I can't get it to work for closures with an async block:</p>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=c0e8210afc5a1c7271064b7607c584b6">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=c0e8210afc5a1c7271064b7607c584b6</a></p>
<p>Could anyone explain what limitation of the compiler is resulting in this error?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>